package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.SmsTemplateMapper;
import com.hq.ims.data.entity.SmsTemplate;
import com.hq.ims.data.vo.SmsTemplateMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.BooleanUtils;
import com.hq.utils.util.StringUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 公司表 服务实现类
 * @since 20241011
 */
@Service
public class BaseSmsTemplateService extends BaseService<SmsTemplateMapper, SmsTemplate> {

    @Resource
    private BaseBusForewarningSetService baseBusForewarningSetService;


    /**
     * 分页
     */
    public List<SmsTemplateMapstruct.SmsTemplateVo> getByPage(
            RequestPage page,
            SmsTemplateMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<SmsTemplate> lq = this.lambdaQuery();
        //排序
        lq.orderByDesc(SmsTemplate::getCreateDateTime);
        //查询条件
        lq.like(StringUtils.isNotEmpty(searchVo.getSmsContext()), SmsTemplate::getSmsContext, searchVo.getSmsContext());

        //执行分页
        lq.page(page);

        List<SmsTemplate> dataList = page.getRecords();
        List<SmsTemplateMapstruct.SmsTemplateVo> voList = SmsTemplateMapstruct.INSTANCE.toSmsTemplateVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public SmsTemplate addData(SmsTemplateMapstruct.SaveVo vo) {

        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);
        BooleanUtils.isTrueThrow(this.isExistsSmsCode(vo.getSmsCode(), null), "短信Code已经存在");

        //账值新的对象
        SmsTemplate entity = new SmsTemplate();
        entity.setSmsCode(vo.getSmsCode());
        entity.setSmsTitle(vo.getSmsTitle());
        entity.setSmsContext(vo.getSmsContext());

        this.save(entity);
        return entity;
    }

    /**
     * 编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public SmsTemplate editData(SmsTemplateMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdEdit.class);
        BooleanUtils.isTrueThrow(this.isExistsSmsCode(vo.getSmsCode(), vo.getSmsCode()), "短信Code已经存在");
        //账值新的对象
        SmsTemplate entity = this.getById(vo.getSmsCode());
        if (entity == null) {
            baseBusForewarningSetService.clearBindSms();
            //调用新增接口
            return this.addData(vo);
        }

        entity.setSmsCode(vo.getSmsCode());
        entity.setSmsTitle(vo.getSmsTitle());
        entity.setSmsContext(vo.getSmsContext());

        entity.updateById();
        return entity;
    }

    public boolean isExistsSmsCode(
            String smsCode,
            String excludeSmsCode
    ) {
        return this.lambdaQuery()
                .eq(SmsTemplate::getSmsCode, smsCode)
                .ne(excludeSmsCode != null, SmsTemplate::getSmsCode, excludeSmsCode)
                .count() > 0;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String smsCode) {
        SmsTemplate data = this.getById(smsCode);
        if (data == null) {
            return;
        }
        //将之前绑定的预警设置删除掉
        baseBusForewarningSetService.clearBindSms(smsCode);
        data.deleteById();
    }

    /**
     * 获取原始数据详情
     */
    public SmsTemplateMapstruct.SmsTemplateVo getDetailVo(String smsCode) {
        SmsTemplate smsTemplate = this.getById(smsCode);
        SmsTemplateMapstruct.SmsTemplateVo smsTemplateVo = SmsTemplateMapstruct.INSTANCE.toSmsTemplateVo(smsTemplate);
        return smsTemplateVo;
    }
}